package demo.team.spring3.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import demo.team.spring3.common.Constants;
import demo.team.spring3.to.Login;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object object, Exception exception)
			throws Exception {
		System.out.println("INSIDE afterCompletion METHOD OF INTERCEPTOR");
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object object, ModelAndView modelAndView) throws Exception {
		
		System.out.println("INSIDE POST_HANDLE METHOD OF INTERCEPTOR");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object object) throws Exception {
		
		System.out.println("INSIDE PRE_HANDLE METHOD OF INTERCEPTOR");
		String uri = request.getRequestURI();
		if(!uri.endsWith("login.htm"))
		{
			Login login = (Login)request.getSession().getAttribute("LOGGED_IN");
			if(login == null)
			{
				response.sendRedirect("login.htm");
				return false;
			}
		}
		return true;
	}

}
